package at.ac.univie.zsu.aguataplan.ui;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import at.ac.univie.zsu.aguataplan.domain.Event;
import at.ac.univie.zsu.aguataplan.domain.EventAttribute;
import at.ac.univie.zsu.aguataplan.domain.EventAttributeValueString;
import at.ac.univie.zsu.aguataplan.exception.DatabaseException;
import at.ac.univie.zsu.aguataplan.property.GeneralProperties;
import at.ac.univie.zsu.aguataplan.property.I18nProperties;
import at.ac.univie.zsu.aguataplan.service.EventManager;
import at.ac.univie.zsu.aguataplan.ui.template.IDialogRelationEntry;
import at.ac.univie.zsu.aguataplan.ui.template.RelationEntryPanel;

/**
 * @author gerry
 * 
 */
public class DialogEventRelation extends JDialog implements UIConstants {

	private static final long serialVersionUID = 1L;

	private JFrame parent;
	private GeneralProperties generalProperties;
	private I18nProperties i18nProp;

	private EventManager eventManager;

	// ----------------------------------------------------
	// CONSTRUCTOR
	// ----------------------------------------------------

	public DialogEventRelation(MainWindow parent, GeneralProperties generalProperties) {
		super(parent, true);

		this.parent = parent;
		this.generalProperties = generalProperties;
		this.i18nProp = generalProperties.getI18nProperties();
		this.eventManager = parent.getServices().getEventManager();

		this.setLayout(null);
		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		setLocationByPlatform(true);
		setSize(SPACING + PANEL_SIZE_X_RELATION + SPACING + PANEL_SIZE_X_RELATION + SPACING + SPACING, SPACING
				+ PANEL_SIZE_Y_ENTRY + SPACING + DIALOG_SIZE_Y_HEADER);
		setTitle(i18nProp.getString(EVENTRELATION_HEADER));
		setResizable(false);

		addEntries();

	}

	// ----------------------------------------------------
	// METHODS
	// ----------------------------------------------------

	private void addEntries() {
		// attribute panel
		RelationEntryPanel eventAttributePanel = new RelationEntryPanel(this, generalProperties,
				new IDialogRelationEntry() {

					@Override
					public List<?> init2() {
						List<EventAttribute> eventAttributes = null;
						try {
							eventAttributes = eventManager.getEventAttributeList();
						} catch (DatabaseException e) {
							JOptionPane.showMessageDialog(DialogEventRelation.this,
									i18nProp.getString(EX_EVENTRELATION_LIST_DESC),
									i18nProp.getString(EX_EVENTRELATION), JOptionPane.ERROR_MESSAGE);
						}
						return eventAttributes;
					}

					@Override
					public List<?> init1() {
						List<Event> events = null;
						try {
							events = eventManager.getEventList();
						} catch (DatabaseException e) {
							JOptionPane.showMessageDialog(DialogEventRelation.this,
									i18nProp.getString(EX_EVENTRELATION_LIST_DESC),
									i18nProp.getString(EX_EVENTRELATION), JOptionPane.ERROR_MESSAGE);
						}
						return events;
					}

					@Override
					public List<?> select1(Object obj) {
						Event event = (Event) obj;
						Set<EventAttribute> eventAttributes = event.getRefEventAttribute();
						List<EventAttribute> l = new ArrayList<EventAttribute>(eventAttributes);
						Collections.sort(l);
						return l;
					}

					@Override
					public boolean add(Object obj1, Object obj2) {
						try {
							eventManager.addEventAttributeToEvent((Event) obj1, (EventAttribute) obj2);
						} catch (DatabaseException e) {
							JOptionPane.showMessageDialog(DialogEventRelation.this,
									i18nProp.getString(EX_EVENTRELATION_ADD_DESC),
									i18nProp.getString(EX_EVENTRELATION), JOptionPane.ERROR_MESSAGE);
							return false;
						}
						return true;
					}

					@Override
					public boolean delete(Object obj1, Object obj2) {
						try {
							eventManager.deleteEventAttributeFromEvent((Event) obj1, (EventAttribute) obj2);
						} catch (DatabaseException e) {
							JOptionPane.showMessageDialog(DialogEventRelation.this,
									i18nProp.getString(EX_EVENTRELATION_DELETE_DESC),
									i18nProp.getString(EX_EVENTRELATION), JOptionPane.ERROR_MESSAGE);
							return false;
						}

						return true;
					}

				});

		eventAttributePanel.init(i18nProp.getString(EVENTRELATION_EVENTEVENTATTRIBUTE), PANEL_SIZE_Y_ENTRY);

		// attributeValueString panel
		RelationEntryPanel eventAttributeValueStringPanel = new RelationEntryPanel(this, generalProperties,
				new IDialogRelationEntry() {

					@Override
					public List<?> select1(Object obj) {
						EventAttribute eventAttribute = (EventAttribute) obj;
						// List<EventAttributeValue> l = new
						// ArrayList<EventAttributeValue>(
						// eventAttribute.getRefEventAttributeValue());
						// Collections.sort(l);
						List<EventAttributeValueString> l = null;
						try {
							l = eventManager.getEventAttributeValueStringListByEventAttribute(eventAttribute);
						} catch (DatabaseException e) {
							JOptionPane.showMessageDialog(DialogEventRelation.this,
									i18nProp.getString(EX_EVENTRELATION_LIST_DESC),
									i18nProp.getString(EX_EVENTRELATION), JOptionPane.ERROR_MESSAGE);
						}

						return l;
					}

					@Override
					public List<?> init2() {
						List<EventAttributeValueString> eventAttributeValueStrings = null;
						try {
							eventAttributeValueStrings = eventManager.getEventAttributeValueStringList();
						} catch (DatabaseException e) {
							JOptionPane.showMessageDialog(DialogEventRelation.this,
									i18nProp.getString(EX_EVENTRELATION_LIST_DESC),
									i18nProp.getString(EX_EVENTRELATION), JOptionPane.ERROR_MESSAGE);
						}
						return eventAttributeValueStrings;
					}

					@Override
					public List<?> init1() {
						List<EventAttribute> eventAttributes = null;
						try {
							eventAttributes = eventManager.getEventAttributeList();
						} catch (DatabaseException e) {
							JOptionPane.showMessageDialog(DialogEventRelation.this,
									i18nProp.getString(EX_EVENTRELATION_LIST_DESC),
									i18nProp.getString(EX_EVENTRELATION), JOptionPane.ERROR_MESSAGE);
						}
						return eventAttributes;
					}

					@Override
					public boolean delete(Object obj1, Object obj2) {
						try {
							eventManager.deleteEventAttributeValueStringFromEventAttribute((EventAttribute) obj1,
									(EventAttributeValueString) obj2);
						} catch (DatabaseException e) {
							JOptionPane.showMessageDialog(DialogEventRelation.this,
									i18nProp.getString(EX_EVENTRELATION_DELETE_DESC),
									i18nProp.getString(EX_EVENTRELATION), JOptionPane.ERROR_MESSAGE);
							return false;
						}

						return true;
					}

					@Override
					public boolean add(Object obj1, Object obj2) {
						try {
							eventManager.addEventAttributeValueStringtoEventAttribute((EventAttribute) obj1,
									(EventAttributeValueString) obj2);
						} catch (DatabaseException e) {
							JOptionPane.showMessageDialog(DialogEventRelation.this,
									i18nProp.getString(EX_EVENTRELATION_ADD_DESC),
									i18nProp.getString(EX_EVENTRELATION), JOptionPane.ERROR_MESSAGE);
							return false;
						}
						return true;
					}
				});
		eventAttributeValueStringPanel.init(i18nProp.getString(EVENTRELATION_EVENTATTRIBUTEEVENTATTRIBUTEVALUESTRING),
				PANEL_SIZE_Y_ENTRY);

		eventAttributePanel.addSibling(eventAttributeValueStringPanel);
		eventAttributeValueStringPanel.addSibling(eventAttributePanel);

		eventAttributePanel.setLocation(SPACING, SPACING);
		eventAttributeValueStringPanel.setLocation(SPACING + PANEL_SIZE_X_RELATION + SPACING, SPACING);

		this.add(eventAttributePanel);
		this.add(eventAttributeValueStringPanel);

	}
}
